// pages/like/like.js
const util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newList: [],
    f_newList:[],
    d_newList: [],
    param: {
      'type': 'GetAll',
      'page': 1,
      'count': 6,
      'tbl': 'minjianbaitai'
    },
    d_param: {
      'content_type': '3',
      'page': 1,
      'page_size':6,
      'uid': 'U5b1e8bc7d3ceb'
    },
    is_artice: true,
    is_login: true,  //是否登录  ,true登录
    login_text: '请登录',
    fabulous_list:[],
    is_info:false,
    num:0,
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showNavigationBarLoading();
    //登录
    if (app.globalData.is_login == false) {
      this.setData({
        is_login: app.globalData.is_login
      })
      return
    }
    //调用后台接口获取点赞
    var backstage_url = app.url.backstage + 'user/fabulous-list';
    var user_id = wx.getStorageSync('uid');
    console.log(this.data.is_artice, '11')
    var back_param = {
      'user_id': user_id,
      'type': 1,
      'page': this.data.page,
    }
    util.http(backstage_url, this.fabulousList, back_param)
  },

  //登录
  login: function (userinfo) { // 需要一个参数来额外接收用户数据
    this.setData({
      is_login: app.globalData.is_login
    })
    if (userinfo.detail.errMsg == 'getUserInfo:ok') {
      this.setData({
        login_text: '正在登录中...'
      })
    }
    util.login((res) => {
      console.log(res, '回调')
      if (res.errMsg == 'request:ok') {
        app.globalData.is_login = true
        this.setData({
          is_login: true
        })
        this.onLoad();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //  this.setData({
    //    newList:[],
    //    page:1
    //  })
  },
  
  //跳转到详情
  onDetail: function (event) {
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../articel-detail/articel-detail?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page=this.data.page+1;
    this.setData({
      page:page,
    })
    this.onLoad()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //后台返回点赞数据
  fabulousList:function(res){
    console.log(res,'a_id')
    let newList = res.data.data.list;
    if (this.data.newList.length > 0 && this.data.newList != undefined) {
      newList = this.data.newList.concat(newList)
    } else {
      newList = newList;
    }
    this.setData({
      newList: newList,
      is_info:true,
    })
    setTimeout(function () { wx.hideNavigationBarLoading(); }, 1000);
  },

  //获取点赞数据
  newResponse: function (res) {
    let newList = res.data;
    let f_newList = {};
    if (this.data.newList.length>0) {
      console.log(1)
      f_newList = this.data.newList.concat(newList)
    } else {
      f_newList = newList;
    }
    this.setData({
      newList: f_newList,
      is_info: true,
    })
  },
  changTab: function (event) {
    let that = this;
    let view = event.currentTarget.dataset.view;
    if (view === '1') {
      this.setData({
        is_artice: true
      })
    } else {
      if (that.data.num==0){
        wx.showNavigationBarLoading();
      }
        //获取点赞段子列表 
        this.setData({
          is_info:false
        })
      var user_id = wx.getStorageSync('uid');
      var back_param = {
        'user_id': user_id,
        'type': 2,
      }
      var url = app.url.backstage + 'user/fabulous-list';
      util.http(url, function (res) {
        let newList = res.data.data.list
        console.log(res.data.data.list)
        console.log(newList);
        that.setData({
          d_newList: newList,
          is_info:true,
          num:1,
        })
        setTimeout(function () { wx.hideNavigationBarLoading(); }, 1000);
      }, back_param, 'GET')

      this.setData({
        is_artice: false
      })
    }
    
  },
  //跳转到段子
  goDuanzi:function(e){
    let id = e.currentTarget.dataset.id;
    wx.reLaunch({
      url: '../duanzi/duanzi?id=' + id
    })
  },

  nextArticle: function () {
    let rdm2 = Math.ceil(Math.random() * 10000)
    this.data.d_param.page = rdm2;
    var url = app.url.video;
    util.http(url, this.d_newResponse, this.data.d_param, 'POST')
  },
  d_newResponse: function (res) {
    let newList = res.data.content.data
    this.setData({
      d_newList: newList,
    })
  },

})